<?php
/**
 * Introduction: 活动表商品表  activity_product_list
 */

namespace Common\Model;


use Think\Model;

class ActivityProductListModel extends Model
{
    //定义数据库连接信息
    protected $connection;

    //定义表名
    protected $tableName = 'activity_product_list';

    /**
     * 初始化
     */
    public function _initialize()
    {

        $this->connection = C('Databaese');

    }

    /**
     * 获取商品列表
     */
    public function getlist($activity_id){
        $fields = ['ps.id','ps.pid','ps.pname','ps.promotion_tips','ps.pic','ps.stock_shop','ps.ms_stock_supplierid', 'ps.ms_entrepot_supplierid', 'ps.ms_direct_supplierid', 'ps.ms_price', 'ps.price', 'ps.market_price', 'ps.ms_discount', 'ps.status', 'ps.sell_amount', 'ps.guojia', 'ps.guojiapic'];
        $list = $this->alias("ap")->join("left join haiou_product_sku as ps on ap.product_id=ps.pid")->where("ap.activity_id=$activity_id and ap.status=1")->order("ap.displayorder desc")->field($fields)->select();
        $pmodel = new ProductSkuModel();
        foreach($list as $k=>$v){
            $ways = [];
            $way = $pmodel->getShipmentsWay($v['id'], 1, $v['ms_stock_supplierid']);
            if (!empty($way)) {
                $ways[] = $way;
            }

            //是否存在于保税仓库
            $warehouseType = 1;
            $way = $pmodel->getShipmentsWay($v['id'], 2, $v['ms_entrepot_supplierid']);
            if (!empty($way)) {
                $ways[] = $way;
            }

            //是否存在于直邮仓
            $warehouseType = 1;
            $way = $pmodel->getShipmentsWay($v['id'], 3, $v['ms_direct_supplierid']);
            if (!empty($way)) {
                $ways[] = $way;
            }            $pic = explode(',',$v['pic']);
            $list[$k]['pic'] = $pic[0];
            $list[$k]['supplierId'] = $ways[0]['supplierId'];
            $list[$k]['stock'] = $ways[0]['stock'];
            $list[$k]['warehouseType'] = $ways[0]['warehouseType'];
        }
        $model = new ActivityModel();
        $info = $model->where("id=$activity_id")->field("title,desc,pic,guojia,guojiapic")->find();
        $res = ['info'=>$info,'list'=>$list];
        return $res;
    }


}